﻿using System;
using System.Data;
using NUnit.Framework;
using UseCaseManager.BO;
using UseCaseManager.Logic;
using UseCaseManager.Test.BO;

namespace UseCaseManager.Test.Logic
{
    [TestFixture]
    public class TestCreateAndExportTable : TestBase
    {
        [Test]
        public void Test_GetDataTableRelease_ReturnsValidTable()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateSavedRelease();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DataTable dataTable = CreateAndExportTable.GetDataTable(release);
            //---------------Test Result -----------------------
            const int expectedNumberOFColumns = 7;
            Assert.AreEqual(expectedNumberOFColumns, dataTable.Columns.Count);
            int index = 0;
            Assert.AreEqual("ReleaseID", dataTable.Columns[index].ColumnName);
            index++;
            Assert.AreEqual("Name", dataTable.Columns[index].ColumnName);
            index++;
            Assert.AreEqual("StartDate", dataTable.Columns[index].ColumnName);
            index++;
            Assert.AreEqual("EndDate", dataTable.Columns[index].ColumnName);
            index++;
            Assert.AreEqual("-PointsBaseline-", dataTable.Columns[index].ColumnName);
            index++;
            //Assert.AreEqual("-CumulativePointsBaselineRemaining-", dataTable.Columns[index].ColumnName);
            //index++;
            Assert.AreEqual("-PointsAssigned-", dataTable.Columns[index].ColumnName);
            index++;
            //Assert.AreEqual("-CumulativePointsAssignedRemaining-", dataTable.Columns[index].ColumnName);
            //index++;
            Assert.AreEqual("-PointsCompleted-", dataTable.Columns[index].ColumnName);
            //index++;
            //Assert.AreEqual("-CumulativePointsCompletedRemaining-", dataTable.Columns[index].ColumnName);
        }

        [Test]
        public void Test_GetDataTableRelease_ReturnsValidTable_WithTwoRows()
        {
            //---------------Set up test pack-------------------
            Drop drop1;
            Drop drop2;
            Release release = TestUtilsRelease.CreateRelease2Drops_FirstDrop_3UseCases_SecondDrop_2UseCases(out drop1, out drop2);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DataTable dataTable = CreateAndExportTable.GetDataTable(release);
            //---------------Test Result -----------------------
            const int numberOfRowsExpected = 2;
            Assert.AreEqual(numberOfRowsExpected, dataTable.Rows.Count);
        }

        [Test]
        public void Test_ExportToCSVRelease_NotSettingRelease_ThrowsException()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                CreateAndExportTable.ExportToCSV((Release)null);
                Assert.Fail("Should throw an exception when release is null");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("release", ex.Message);
            }
        }

        public void Test_GetDataTableProject_NoReleases_ReturnsBlankCSV()
        {
            //---------------Set up test -----------------------
            Project project = TestUtilsProject.CreateSavedProject();
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            DataTable dataTable = CreateAndExportTable.GetDataTable(project);
            //---------------Test Result -----------------------
            Assert.IsNull(dataTable);
        }

        [Test]
        public void Test_GetDataTableProject_ReturnsValidTable()
        {
            //---------------Set up test -----------------------
            Project project = TestUtilsProject.CreateUnsavedValidProject();
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            project.Releases.Add(release);
            project.Save();
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            DataTable dataTable = CreateAndExportTable.GetDataTable(project);
            //---------------Test Result -----------------------
            const int expectedNumberOfColumns = 7;
            Assert.AreEqual(expectedNumberOfColumns, dataTable.Columns.Count);
        }

        [Test]
        public void Test_GetDataTableProject_ReturnsValidTable_WithFiveRows()
        {
            //---------------Set up test -----------------------
            Project project =TestUtilsProject.GetComplexSavedProject();
            //---------------Assert pre conditions -------------
            Assert.AreNotEqual(0, project.PointsAssigned);
            //---------------Execute Test ----------------------
            DataTable dataTable = CreateAndExportTable.GetDataTable(project);
            //---------------Test Result -----------------------
            const int expectedNumberOfRows = 5;
            Assert.AreEqual(expectedNumberOfRows, dataTable.Rows.Count);
        }

        [Test]
        public void Test_ExportToCSVProject_NotSettingProject_ThrowsException()
        {
            //---------------Set up test -----------------------
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            try
            {
                CreateAndExportTable.ExportToCSV((Project)null);
                Assert.Fail("Should throw an exception when project is null");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("project", ex.Message);
            }
        }

        [Test, Ignore("not actual test- used to see the csv")]
        public void Test_ExportToCSVProject_CreatesValidCSV()
        {
            //---------------Set up test -----------------------
            Project project =TestUtilsProject.GetComplexSavedProject();
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            CreateAndExportTable.ExportToCSV(project);
            //---------------Test Result -----------------------

        }

        [Test, Ignore("not actual test- used to see the csv")]
        public void Test_ExportToCSVRelease_CreateValidCSV()
        {
            //---------------Set up test -----------------------
            Drop drop1;
            Drop drop2;
            Release release = TestUtilsRelease.CreateRelease2Drops_FirstDrop_3UseCases_SecondDrop_2UseCases(out drop1, out drop2);
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            CreateAndExportTable.ExportToCSV(release);
            //---------------Test Result -----------------------
        }

        [Test]
        public void Test_GetCompletionStatusTable_ReturnsValidTable()
        {
            //---------------Set up test pack-------------------
            Drop drop;
            Drop drop2;
            Release release = TestUtilsRelease.CreateRelease2Drops_FirstDrop_3UseCases_SecondDrop_2UseCases(out drop, out drop2);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DataTable dataTable = CreateAndExportTable.GetCompletionStatusTable(release);
            //---------------Test Result -----------------------
            const int expectedNumberOfColumns = 4;
            Assert.AreEqual(expectedNumberOfColumns, dataTable.Columns.Count);
            int index = 0;
            Assert.AreEqual("UseCaseName", dataTable.Columns[index].ColumnName);
            index++;
            Assert.AreEqual("PointsAssigned", dataTable.Columns[index].ColumnName);
            Assert.IsTrue(dataTable.Columns.Contains("Drop1 Name"));
            Assert.IsTrue(dataTable.Columns.Contains("Drop2 Name"));
        }
    }
}